#!/usr/bin/env python

import os.path
import logging

import matplotlib 
matplotlib.use('GTKAgg')
from matplotlib.patches import FancyArrow
from matplotlib.figure import Figure 
from matplotlib.axes import Subplot 
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.backends.backend_gtkagg import NavigationToolbar
#from matplotlib.numerix import arange, sin, pi 
from gtkmvc.view import View

from i18n.conf import _

GLADE_NAME = 'main_interface.glade'
GLADE_PATH = os.path.join('.', 'glade')
GLADE = os.path.join(GLADE_PATH, GLADE_NAME)

log = logging.getLogger('views')

class Arrow(FancyArrow):
    def __init__(self, x, y, dx, dy, *args, **kw):
        self._update_coords(x, y, dx, dy)
        self.args = args
        self.kw = kw
        FancyArrow.__init__(self, x, y, dx, dy, *args, **kw)
    def _update_coords(self, x, y, dx, dy):
        self.x, self.y = x, y
        self.dx, self.dy = dx, dy
    def set_verts(self, x, y, dx, dy):
        #self.__init__(x, y, dx, dy, *self.args, **self.kw)
        #self.update_from(Arrow(x, y, dx, dy, *self.args, **self.kw))
        self._update_coords(x, y, dx, dy)
        self.xy = FancyArrow(x, y, dx, dy, *self.args, **self.kw).get_verts()
    def set_dxdy(self, dx, dy):
        self.set_verts(self.x, self.y, dx, dy)
    def set_dx(self, dx):
        self.set_dxdy(dx, self.dy)
    def set_dy(self, dy):
        self.set_dxdy(self.dx, dy)

class MPLGraph(object):
    def __init__(self):
        figure = Figure(figsize=(6,4), dpi=72)
        self.figure = figure
        axis = figure.add_subplot(111)
        self.axis = axis
        canvas = FigureCanvas(figure) # a gtk.DrawingArea
        self.canvas = canvas
        self.background = None
        self.restored = False
        self.setup()
        canvas.show()
    def setup(self):
        axis = self.axis
        axis.set_xlabel(_('Graph1 x'))
        axis.set_ylabel(_('Graph1 y'))
        axis.set_title(_('An Empty Graph 1'))
        axis.grid(True)
        arrow1 = Arrow(0.1, 0.1, 0.5, 0.5, width=0.01, animated=True)
        self.arrow1 = arrow1
        axis.add_patch(arrow1)
    def clear(self):
        if self.background is None:
            self.background = self.canvas.copy_from_bbox(self.axis.bbox)
        self.canvas.restore_region(self.background)
        self.restored = True
    def redraw(self):
        assert self.restored
        for patch in self.axis.patches:
            self.axis.draw_artist(patch)
        self.canvas.blit(self.axis.bbox)
        self.restored = False


class PhasorDiagram(MPLGraph):
    def setup(self):
        axis = self.axis
        axis.grid(True)
        vector_v = Arrow(0.1, 0.1, 0.5, 0.5, width=0.01, animated=True,
            fc='green')
        vector_e = Arrow(0.1, 0.1, 0.5, 0.5, width=0.01, animated=True,
            fc='red')
        self.vector_v = vector_v
        self.vector_e = vector_e
        axis.add_patch(vector_v)
        axis.add_patch(vector_e)


class MainView(View):
    """The application view. Contains only the main window1 tree."""

    def __init__(self, controller):
        """Contructor, takes the controller instance to perform registration"""
        self.graphs = {}

        View.__init__(self, controller, GLADE, 'window1')
    def create_graph1(self, str1, str2, int1, int2):
        log.info(_('Creating %s' % 'graph1'))
        # setup matplotlib stuff on first notebook page
        phasor_diagram = PhasorDiagram()
        self.graphs.update(locals())
        self.phasor_diagram = phasor_diagram
        return phasor_diagram.canvas
    def create_graph2(self, str1, str2, int1, int2):
        log.info(_('Creating %s' % 'graph2'))
        # setup matplotlib stuff on first notebook page
        graph2 = MPLGraph()
        self.graphs.update(locals())
        self.graph2 = graph2
        return graph2.canvas

    def create_graph3(self, str1, str2, int1, int2):
        log.info(_('Creating %s' % 'graph3'))
        # setup matplotlib stuff on first notebook page (empty graph) 
        figure = Figure(figsize=(6,4), dpi=72) 
        axis = figure.add_subplot(111) 
        axis.set_xlabel('Graph3 x') 
        axis.set_ylabel('Graph3 y') 
        axis.set_title('An Empty Graph 3') 
        axis.grid(True) 
        canvas = FigureCanvas(figure) # a gtk.DrawingArea 
        canvas.show()
        return canvas


if __name__ == '__main__':
    #TODO: run tests
    pass

# vim:set shiftwidth=4 tabstop=4 expandtab ci textwidth=79:

